﻿using System.Linq;
using System.Web;
using System.Web.UI.WebControls.WebParts;
using Concret;
using ORM;

/// <summary>
/// DataLogic 的摘要说明
/// </summary>
public static class DataLogic
{
	/// <summary>
	/// 获取当前会话人员
	/// </summary>
	/// <param name="db"></param>
	/// <returns></returns>
	public static Users GetCurrentUser(DbOperation db)
	{
		var (request, response) = (HttpContext.Current.Request, HttpContext.Current.Response);
		var (acc, psw) = (request.Cookies["acc"]?.Value, request.Cookies["psw"]?.Value);

		if (acc == null || psw == null)
			return null;

		var user = new Users(db) { Account_Val = acc, Password_Val = psw }.GetByFVs().SingleOrDefault();
		if (user == null)
		{
			response.Cookies.Remove("acc");
			response.Cookies.Remove("psw");
			return null;
		}

		return user;
	}

	public static bool IsAdmin(this Users user) => user.Uname_Val == "钟老师" || user.Uname_Val == "简汇承";
}